package com.charonmc.utils.mysql;

import javax.annotation.Nullable;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Objects;


/**
 * UpdateUtils是管理与修改数据有关的对象
 * @author ChuYang
 */

public class UpdateUtils extends SqlManager{

    private int sqlRowCount;
    private ArrayList<Object> params = new ArrayList<>();
    private ArrayList<Object> value = new ArrayList<>();

    /**
     * 创建对象
     * @param connection SQLManager传入连接
     */
    public UpdateUtils(Connection connection) {
        this.connection = connection;
    }

    /**
     * 指定数据表
     * @param table 所要操作数据表
     * @return {@link UpdateUtils}
     */
    public UpdateUtils inTable(String table){
        this.sql.append(table)
                .append(" SET ");
        return this;
    }

    /**
     * 创建更改索引
     * @param columns 索引名
     * @return {@link UpdateUtils}
     */
    public UpdateUtils addColumnNames(String[] columns){
        for (int i = 0; i < columns.length - 1; i++) {
            this.sql.append(columns[i])
                    .append("=?,");
        }
        this.sql.append(columns[columns.length - 1])
                .append("=? ");
        return this;
    }

    /**
     * 为索引赋值
     * @param Params 赋值
     * @return {@link UpdateUtils}
     */
    public UpdateUtils addParams(Object[] Params){
        params.addAll(Arrays.asList(Params));
        return this;
    }

    /**
     * 创建约束条件
     * @param limitName 约束索引名
     * @param operator 约束判断符 仅支持">,<,=,!="
     * @param limitValue 约束索引值
     * @return {@link UpdateUtils}
     */
    public UpdateUtils addCondition(String limitName, @Nullable String operator, Object limitValue) {
        if (limitValue instanceof String){
            limitValue = "'" + limitValue + "'";
        }
        value.add(limitValue);
        if (!Objects.equals(operator, "=") && !Objects.equals(operator, "<") && !Objects.equals(operator, ">") && !Objects.equals(operator, "!=")) {
            return this;
        }
        if (sql.indexOf(" WHERE ") == -1) {
            this.sql.append(" WHERE ").append(limitName).append(operator).append("?");
        } else {
            this.sql.append(" & ").append(limitName).append(operator).append("?");
        }
        return this;
    }

    /**
     * 创建相等约束条件
     * @param limitName 约束索引名
     * @param limitValue 约束索引值
     * @return {@link UpdateUtils}
     */
    public UpdateUtils addCondition(String limitName, Object limitValue){
        if (limitValue instanceof String){
            limitValue = "'" + limitValue + "'";
        }
        value.add(limitValue);
        if (sql.indexOf(" WHERE ") == -1) {
            this.sql.append(" WHERE ").append(limitName).append("=").append(limitValue);
        }else {
            this.sql.append(" & ").append(limitName).append("=").append(limitValue);
        }
        return this;
    }

    /**
     * 构建sql语句
     * @return {@link UpdateUtils}
     * @throws SQLException 数据库异常
     */
    public UpdateUtils build() throws SQLException {
        this.ps = connection.prepareStatement(this.sql.toString());
        for (int i = 0; i < params.size(); i++) {
            ps.setObject(i + 1, params.get(i));
        }
        return this;
    }

    /**
     * 执行sql语句
     * @return {@link UpdateUtils}
     * @throws SQLException 数据库异常
     */
    public UpdateUtils execute() throws SQLException {
        sqlRowCount = ps.executeUpdate();
        return this;
    }

    /**
     * 获得受影响的行数
     * @return 受影响的行数
     */
    public int getResult(){
        return this.sqlRowCount;
    }
}
